//
//  TagCell.m
//  quickBatchManager
//
//  Created by jack on 15/9/16.
//  Copyright (c) 2015年 jack. All rights reserved.
//

#import "TagCell.h"

@implementation TagCell

- (void)awakeFromNib {
//    NSString *str  = @"去年今日此门中，人面桃花相映红；人面不知何处去，桃花依旧笑春风。";
//    UIFont* font = [UIFont fontWithName:@"Geeza Pro" size:11];
//    CGSize size = [str sizeWithFont:font constrainedToSize:CGSizeMake(25.0f, 460-44) lineBreakMode:NSLineBreakByWordWrapping];
//    
//    NSLog(@"size.height=%f,%f",size.height,size.width);
//    NSMutableArray* array = [[NSMutableArray alloc]initWithCapacity:0];
//    
//    for (int i = 0; i <  4; i ++) {
//        NSString* substr=[str substringWithRange:NSMakeRange(i*8, 8)];
//        [array addObject:substr];
//    }
//    for (int i = 0;i < array.count; i++) {
//        
//     //   _tagLabel = [[UILabel alloc]initWithFrame:CGRectMake(220-(size.width+15)*(i+1),10, size.width, size.height)];
//        _tagLabel.frame = CGRectMake(_tagLabel.frame.origin.x, _tagLabel.frame.origin.y, size.width, size.height);
//        _tagLabel.text = [array objectAtIndex:i];
//        _tagLabel.numberOfLines = 0;
//        _tagLabel.font = font;
//        _tagLabel.textColor = [UIColor purpleColor];
//        _tagLabel.backgroundColor = [UIColor clearColor];
//        _tagLabel.shadowColor = [UIColor colorWithWhite:0.4 alpha:0.8];
//        _tagLabel.shadowOffset = CGSizeMake(1.0f, 2.0f);
//    }
    
    
    UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];
    longpressGesutre.delegate = self;
    //长按时间为1秒
    longpressGesutre.minimumPressDuration=0.5;
    //允许15秒中运动
    longpressGesutre.allowableMovement=0;
    //所需触摸1次
    longpressGesutre.numberOfTouchesRequired=1;
    [self.contentView addGestureRecognizer:longpressGesutre];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



-(void)handleLongpressGesture:(UILongPressGestureRecognizer *)sender
{
    CGPoint location = [sender locationInView:_mSuperView];
    CGPoint center;
    UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;
    UIGestureRecognizerState state = longPress.state;
    switch (state) {
        case UIGestureRecognizerStateBegan:
            _dragView = [self customSnapshoFromView:self];
            [_dragView setFrame:CGRectMake(location.x - self.frame.size.width/2 , location.y - self.frame.size.height/2, self.frame.size.width, self.frame.size.height)];
            [_mSuperView addSubview:_dragView];
            
            break;
            
        case UIGestureRecognizerStateChanged:
            center = _dragView.center;
            center.y = location.y;
            center.x = location.x;
            _dragView.center = center;
            
            if(CGRectContainsPoint(_goodView.frame, location))
            {
                _goodView.layer.borderWidth = 2;
                _goodView.layer.borderColor = [[UIColor yellowColor] CGColor];
            }else
            {
                _goodView.layer.borderWidth = 0;
            }
            break;
            
        default:
            [UIView animateWithDuration:0.25 animations:^{
                _dragView.transform = CGAffineTransformIdentity;
                _dragView.alpha = 0.0;
                
            } completion:^(BOOL finished) {
                [_dragView removeFromSuperview];
                _dragView = nil;
                _goodView.layer.borderWidth = 0;
                
                if(CGRectContainsPoint(_goodView.frame, location))
                {
                    NSLog(@"添加。。。");
                    if([_delegate respondsToSelector:@selector(tagDragToGoodView:andSaleOrderModel:)])
                    {
                        [_delegate tagDragToGoodView:self  andSaleOrderModel:_data];
                    }
                }
            }];
            break;
    }
    
}


- (UIView *)customSnapshoFromView:(UIView *)inputView {
    
    UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, NO, 0);
    [inputView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIView *snapshot = [[UIImageView alloc] initWithImage:image];
    snapshot.layer.masksToBounds = NO;
    snapshot.layer.cornerRadius = 0.0;
    snapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0);
    snapshot.layer.shadowRadius = 5.0;
    snapshot.layer.shadowOpacity = 0.4;
    
    return snapshot;
}

@end
